import { isNil, nth } from "lodash-es"

export function ipairs<T>(arr: T[]): [number, T][] {
  return arr.map((item, index) => {
    return [index, item]
  })
}

export function next(arr:any[]){
  let res = null
  arr.some((item,index)=>{
    if(!isNil(item)){
      res = [index,item]
     return true
    }
    return false
  })
 return  res
}
// export function pairs<T, P extends T[] | Record<number, T>>(arr: P): [number, T][] {
//   if (arr instanceof Array) {
//     return arr.map((item, index) => {
//       return [index, item]
//     })
//   } else {
//     let res: [number, T][] = []
//     let index = 0
//     for (let i in arr) {
//       res[index] = [+i, arr[i]]
//       index++
//     }
//     return res
//   }
// }

type Y<T> = T extends Record<number, infer A> ? [number,A][]:never

export function pairs<T extends Record<number,any>>(arr: T) {
    let res = [] 
    let index = 0
    for (let i in arr) {
      res[index] = [+i, arr[i]]
      index++
    }
    return res as T extends Record<number, infer A> ?[number,A][]: never
}

export function select<T>(index:number,arr:T[]):T|undefined {
return nth(arr,index -1)
}


export function stringFormat(format:string,...args:string[]){
  let index = 0
   return format.replace(/%s/g, ()=>{
     return args[index++] || ''
   })
 }

 export function stringGsub(data:string,searchValue: string | RegExp, replaceValue: string | ((substring:string,...args:any[])=>string)){
  if(typeof replaceValue === 'string' ){
    return data.replaceAll(searchValue,replaceValue)
  }else{
    return data.replaceAll(searchValue,replaceValue)
  }
 }

 export function stringSplit (str:string,value:string){
   return str.split(value)
 }